// 导出页面为PDF格式
import html2Canvas from "html2canvas";
import JsPDF from "jspdf";
import $ from "jquery";

export default {
  install(Vue, options) {

    Vue.prototype.getPdf = async function (dom, isTransverse) {
      let canvas = await html2Canvas(document.querySelector(dom), {
        allowTaint: true,
        background: "#FFFFFF"
      });
      let leftHeight = canvas.height;
      let position = 0; //页面偏移
      let a4Width = 595.28;
      let a4Height = 841.89;
      let a4HeightRef = Math.floor((canvas.width / a4Width) * a4Height); //1页pdf显示html页面的高度
      let pageData = canvas.toDataURL("image/jpeg", 1.0);
      let pdf = new JsPDF("", "pt", "a4");
      let index = 1;
      let canvas1 = document.createElement("canvas");
      let height;
      pdf.setDisplayMode("fullwidth", "continuous", "FullScreen");

      if (leftHeight < a4HeightRef) {
        pdf.addImage(pageData, "JPEG", 0, 20, a4Width, (a4Width / canvas.width) * leftHeight);
        console.warn(123);
      } else {
        try {
          pdf.deletePage(0);
          createImpl(canvas, a4Width, leftHeight, a4HeightRef, position, index, height, canvas1, pdf);
        } catch (err) {
          console.error(err);
        }
      }
      return pdf;
    };
    function createImpl(canvas, a4Width, leftHeight, a4HeightRef, position, index, height, canvas1, pdf) {
      if (leftHeight > 0) {
        index++;
        let checkCount = 0;
        if (leftHeight > a4HeightRef) {
          let i = position + a4HeightRef;
          for (i = position + a4HeightRef; i >= position; i--) {
            let isWrite = true;
            for (let j = 0; j < canvas.width; j++) {
              let c = canvas.getContext("2d").getImageData(j, i, 1, 1).data;
              if (c[0] != 0xff || c[1] != 0xff || c[2] != 0xff) {
                isWrite = false;
                break;
              }
            }
            if (isWrite) {
              checkCount++;
              if (checkCount >= 10) {
                break;
              }
            } else {
              checkCount = 0;
            }
          }
          height = Math.round(i - position) || Math.min(leftHeight, a4HeightRef);
          if (height <= 0) {
            height = a4HeightRef;
          }
        } else {
          height = leftHeight;
        }

        canvas1.width = canvas.width;
        canvas1.height = height;

        let ctx = canvas1.getContext("2d");
        ctx.drawImage(canvas, 0, position, canvas.width, height, 0, 0, canvas.width, height);
        if (position != 0) {
          pdf.addPage();
        }
        pdf.addImage(canvas1.toDataURL("image/jpeg", 1.0), "JPEG", 0, 20, a4Width, (a4Width / canvas1.width) * height);
        leftHeight -= height;
        position += height;
        $(".pdfProgress").text(index + 1);
        $(".pdfTotal").text(index + Math.ceil(leftHeight / a4HeightRef));
        if (leftHeight > 0) {
          return createImpl(canvas, a4Width, leftHeight, a4HeightRef, position, index, height, canvas1, pdf);
        }
        pdf.save()
      }
    }

  }
};
